/*请求*/
import App from '@/App.vue'
var baseUrl
switch (App.globalData.version) {
	case "dev":
		baseUrl = ""; //测试
		//测试
		break;
	case "uat":
		//正式
		baseUrl = "https://signup.yfzxzjysg.top/"; //正式 
		break;
	case "localhost":
		//本地
		baseUrl = ""; //本地

		break;
	default:
		baseUrl = ""; //测试
		break;
}
// post请求
function doPost(url, parm, success, fail, complete) {
	// 转圈
	uni.showLoading({
		title: "加载中"
	})
	let fullUrl = baseUrl + url;
	let token = uni.getStorageSync("token") ? uni.getStorageSync("token") : "";
	let p = parm || {};
	uni.request({
		url: fullUrl, //仅为示例，并非真实接口地址。
		data: p,
		method: "POST",
		header: {
			// 自定义请求头信息
			'content-type': 'application/json',
			Accept: 'application/json, text/plain, */*'
		},
		success: (res) => {
			uni.hideLoading()
			if (success) {
				if (res.data.status !== 200) {
					// uni.showToast({
					// 	title: res.data.message,
					// 	icon: 'none',
					// 	duration: 2000,
					// 	mask: true,
					// })
					success(res.data);
				} else {
					success(res.data);
				}
			}
		},
		// fail: (res) => {
		// 	uni.showToast({
		// 		title: '服务器繁忙，请稍后再试!',
		// 		icon: 'none',
		// 		duration: 1500,
		// 		mask: true,
		// 	})
		// }
	});
}
//get请求
function doGet(url, parm, success, fail, complete) {
	// 转圈
	uni.showLoading({
		title: "加载中"
	})
	let fullUrl = baseUrl + url;
	let token = uni.getStorageSync("token") ? uni.getStorageSync("token") : "";
	let p = parm || {};
	uni.request({
		url: fullUrl, //仅为示例，并非真实接口地址。
		data: p,
		method: "GET",
		header: {
			// 自定义请求头信息
			'content-type': 'application/json',
			Accept: 'application/json, text/plain, */*'
		},
		success: (res) => {
			uni.hideLoading()
			if (success) {
				if (res.data.status == 200) {
					success(res.data);
					// uni.showToast({
					// 	title: '请求成功',
					// 	icon: 'none'
					// })
				} else {
					// uni.showToast({
					// 	title: res.data.message,
					// 	icon: 'none',
					// 	duration: 1000,
					// 	mask: true,
					// })
					success(res.data);
				}
			}
		},
		// fail:(res)=>{
		// 	uni.showToast({
		// 		title: '服务器繁忙，请稍后再试!',
		// 		icon: 'none',
		// 		duration: 1500,
		// 		mask: true,
		// 	})
		// }
	});
}

/*加载中*/
function showLoading(msg) {
	uni.showLoading({
		title: msg
	});
}

/*弹框*/
// function showToast(msg, type) {
// 	uni.showToast({
// 		title: msg,
// 		icon: type ? type : 'none',
// 		mask: true,
// 		duration: 2000
// 	});
// }

/*百分比*/
function Percentage(num, total) {
	if (num == 0 || total == 0) {
		return 0;
	}
	return (Math.round(num / total * 10000) / 100.00); // 小数点后两位百分比
}

/**/
function showModal(title, content, success, fail) {
	uni.showModal({
		title: title,
		content: content,
		success: function(res) {
			if (res.confirm) {
				success();
			} else if (res.cancel) {
				fail();
			}
		}
	});
}

/*手机号验证*/
function checkMobile(num) {
	let myreg = /^1[3456789]\d{9}$/;
	return myreg.test(num);
}

/* 返回二维码图片 */
function qrcode(url) {
	//let toolUrl = "https://wechat.weixinzjit.com/byyx/index.php/api/common/qrcode?text=";
	let toolUrl = "https://api.qrserver.com/v1/create-qr-code?data=";
	return toolUrl + encodeURIComponent(url);
}

/* 获取url参数 */
function getUrlParam(name) {
	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1]
		.replace(/\+/g, '%20')) || ""
}


module.exports = {
	doPost,
	doGet,
	showLoading,
	// showToast,
	Percentage,
	showModal,
	checkMobile,
	qrcode,
	getUrlParam,
	baseUrl
}
